rm(list=ls())

pacman::p_load(lubridate, tidyverse, haven, gtrendsR, rdrobust, rddtools,
               broom, ggpubr, gridExtra,readxl)

# meta analyses race of victim
setwd('put_your_wd_here')
files <- 'datasets/metaanalysis'
files <- list.files(files,full.names = T)
dat <- map(files, read_csv)

# recode vars
dat[[1]] <- dat[[1]] %>% unite(col='x', c('z', 'y'),sep = ' PAC ')
dat[[1]]$category <- 'Donations'
dat[[2]]$category <- 'Google Trends'
dat[[2]]$x <- dat[[2]]$z
dat[[3]]$category <- 'Petitions'
dat[[3]]$x <- dat[[3]]$z
dat[[4]]$x <- dat[[4]]$category
dat[[4]]$category <- 'Twitter'

# combine and recode tercile
dat <- bind_rows(dat)
dat$tercile <- gsub('Meta Estimate \\(|\\)','',dat$shooting)
dat$tercile[dat$tercile == 'Lower'] <- 'Bottom'
dat %>%
  mutate(category = factor(category, levels=c(
    'Google Trends','Twitter','Petitions','Donations'
  )),
  x = factor(x, levels=rev(c(
    'Gun Control','Gun Rights',
    'Gun Control Orgs','Gun Rights Orgs',
    '#guncontrol','#gunrights',
    '#everytown','#NRA',
    'Giffords PAC Amount','Giffords PAC Number',
    'NRA PAC Amount','NRA PAC Number'
  )))) %>%
  ggplot(aes (x=x, y=Coef, ymin = lower, ymax=upper, shape=tercile)) +
  geom_hline(yintercept=0, linetype=2, color='red')+
  geom_pointrange(position=position_dodge(width=0.8), fill='white') +
  facet_wrap(~category, scales='free_y', ncol=1) +
  coord_flip() +
  scale_shape_manual(values=c(21, 22, 23),
                     breaks = c('Top','Middle','Bottom'),
                     name='Pct Victims\nWhite\n(Terciles)') +
  theme_bw() +
  labs(y='Pooled Effects',x='')
ggsave(width=6, height=8, 
       filename = 'figures/meta_analyses_by_race.pdf')

